package com.exam.project.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.exam.project.business.domain.Question;
import com.exam.project.business.domain.UserExam;
import com.exam.project.business.dto.*;
import io.jsonwebtoken.lang.Assert;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.exam.framework.aspectj.lang.annotation.Log;
import com.exam.framework.aspectj.lang.enums.BusinessType;
import com.exam.project.business.domain.Exam;
import com.exam.project.business.service.IExamService;
import com.exam.framework.web.controller.BaseController;
import com.exam.framework.web.domain.AjaxResult;
import com.exam.common.utils.poi.ExcelUtil;
import com.exam.framework.web.page.TableDataInfo;

/**
 * 考试表Controller
 * 
 * @author qiuwenbing
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/business/exam")
public class ExamController extends BaseController
{
    @Autowired
    private IExamService examService;

    /**
     * 查询考试表列表
     */
    @PreAuthorize("@ss.hasPermi('business:exam:list')")
    @GetMapping("/list")
    public TableDataInfo list(Exam exam)
    {
        startPage();
        List<Exam> list = examService.selectExamList(exam);
        return getDataTable(list);
    }

    @GetMapping("/listNoPage")
    public AjaxResult listNoPage(Exam exam)
    {
        List<Exam> list = examService.selectExamList(exam);
        return AjaxResult.success(list);
    }

    /**
     * 导出考试表列表
     */
    @PreAuthorize("@ss.hasPermi('business:exam:export')")
    @Log(title = "考试表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Exam exam)
    {
        List<Exam> list = examService.selectExamList(exam);
        ExcelUtil<Exam> util = new ExcelUtil<Exam>(Exam.class);
        util.exportExcel(response, list, "考试表数据");
    }

    /**
     * 获取考试表详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:exam:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(examService.selectExamById(id));
    }

    /**
     * 新增考试表
     */
    @PreAuthorize("@ss.hasPermi('business:exam:add')")
    @Log(title = "考试表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Exam exam)
    {
        return toAjax(examService.insertExam(exam));
    }

    /**
     * 修改考试表
     */
    @PreAuthorize("@ss.hasPermi('business:exam:edit')")
    @Log(title = "考试表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Exam exam)
    {
        return toAjax(examService.updateExam(exam));
    }

    /**
     * 删除考试表
     */
    @PreAuthorize("@ss.hasPermi('business:exam:remove')")
    @Log(title = "考试表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(examService.deleteExamByIds(ids));
    }

    @GetMapping("myExam")
    public AjaxResult myExam(){
        Exam info = examService.myExam();
        return AjaxResult.success(info);
    }

    /**
     * 查询考试题目
     * @return
     */
    @GetMapping("myExamInfo/{examId}")
    public AjaxResult myExamInfo(@PathVariable Long examId){
        ExamVo vo = examService.selectMyExamList(examId);
        return AjaxResult.success(vo);
    }

    @GetMapping("queryExamInfo")
    public AjaxResult myExamInfo(UserExam exam){
        ExamVo vo = examService.selectExamInfo(exam.getExamId(),exam.getUserId());
        return AjaxResult.success(vo);
    }

    @PostMapping("submitScore")
    public AjaxResult submitScore(@RequestBody SubmitScoreDto dto){
        Assert.notNull(dto.getExamId());
        Assert.notNull(dto.getUserId());
        examService.submitExamScore(dto);
        return AjaxResult.success();
    }

    @GetMapping("queryChoose")
    public TableDataInfo queryChoose(Question dto){
        startPage();
        List<UserDto> list = examService.queryChoose(dto.getExamId());
        TableDataInfo dataTable = getDataTable(list);
        dataTable.getRows().forEach(item->{
            UserDto user = (UserDto) item;
            // 是否选中
            int choose = examService.queryIsChoose(user.getUserId(),dto.getExamId());
            user.setChoose(choose);
        });
        return dataTable;
    }

    @PostMapping("submitExam")
    public AjaxResult submitExam(@RequestBody ExamAnswerDto dto){
        // 提交考试答案

        examService.submitExam(dto);
        return AjaxResult.success();
    }

}
